package cli

import (
	"fmt"
	"time"

	cli "github.com/urfave/cli/v2"
)

var InfoCmd = &cli.Command{
	Name:  "info",
	Usage: "显示当前服务端信息",
	Action: func(cctx *cli.Context) error {
		api, closer, err := GetServerAPI()
		if err != nil {
			return err
		}
		defer closer()

		v := api.Version()
		acnt := api.GetALiveConn()
		cinfo := api.ProxyConnsInfo()
		s := api.GetSlaveInfo()
		sinfo := api.SlaveConnsInfo()

		fmt.Printf("服务端版本信息\t\t\t\t:\t%s\n", v)
		fmt.Printf("代理连接数  \t\t\t\t:\t%d\n", acnt)
		fmt.Println("代理连接详情:")
		for k, v := range cinfo {
			fmt.Printf("%-40s\t:\t%d\n", k, v)
		}

		fmt.Printf("映射连接数量:  \t\t\t\t:\t%d\n", len(sinfo))
		for k, v := range s {
			fmt.Printf("%-40s\t:\t%s\n", k, v)
		}
		fmt.Println("映射连接详情:")
		for _, v := range sinfo {
			fmt.Printf("%-40s\t:\t%s\n", v.Laddr, time.Now().Sub(v.Begin).String())
		}
		return nil
	},
}
